/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Device.h"
REGISTER_PLUGIN_CLASS(Device)
bool Device::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "getDeviceInfo") {
        m_cbc = cbc;
        if(m_strModel == "") {
            initialize(cbc);
            return true;
        }
        sendResult();
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    
    if(action == "getScaleFont") {
        m_cbc2 = cbc;
        executeArkTs(action, 0,"", "Device", cbc);
    }
    
    if(action == "setScaleFont") {
        m_cbc2 = cbc;
        double dbScale = cJSON_GetArrayItem(args, 0)->valuedouble;
        char szBuf[32];
        sprintf(szBuf, "%0.2f", dbScale);
        executeArkTs(action, 0, szBuf, "Device", cbc);
    }
    return true;
}

void Device::sendResult()
{
    cJSON* json = cJSON_CreateObject();
    cJSON_AddStringToObject(json, "version", m_strVersion.c_str());
    cJSON_AddStringToObject(json, "platform", m_strPlatform.c_str());
    cJSON_AddStringToObject(json, "model", m_strModel.c_str());
    cJSON_AddStringToObject(json, "manufacturer", m_strManufacturer.c_str());
    if(m_strModel == "emulator") {
        cJSON_AddStringToObject(json, "uuid", "emulator123456");
        cJSON_AddTrueToObject(json, "isVirtual");
        cJSON_AddStringToObject(json, "serial", "emulator123456");
    } else {
        cJSON_AddStringToObject(json, "uuid", m_strUuid.c_str());
        cJSON_AddFalseToObject(json, "isVirtual");
        cJSON_AddStringToObject(json, "serial", m_strSerial.c_str());
    }
    cJSON_AddStringToObject(json, "sdkVersion", m_strSdkVersion.c_str());
    m_cbc.success(json);
    cJSON_Delete(json);
}

void Device::initialize(CallbackContext cbc)
{
    executeArkTs("/pages/GetDeviceInfo", 0, "", "Device", cbc);
    return;
}

bool Device::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
     if(content == "getScaleFont") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string strScale = cJSON_GetArrayItem(json, 0)->valuestring;
                double dbScale = atof(strScale.c_str());
                PluginResult pluginResult(PluginResult::Status::OK, dbScale);
                m_cbc2.sendPluginResult(pluginResult);
            } else {
                m_cbc2.error("please manually open it in the notification");
            }
        }
        return true;
    }
    if(content == "setScaleFont") {
        if(json != NULL && json->type == cJSON_Array) {
            PluginResult pluginResult(PluginResult::Status::OK);
            m_cbc2.sendPluginResult(pluginResult);
        }
        return true;
    }
    
    if(json != NULL && json->type == cJSON_Array) {
       int count = cJSON_GetArraySize(json);
       for(int i=0; i<count; i++) {
            switch(i) {
            case 0:
                m_strUuid = cJSON_GetArrayItem(json,i)->valuestring;
                break;
            case 1:
                m_strVersion = cJSON_GetArrayItem(json,i)->valuestring;
                break;
            case 2:
                m_strPlatform = cJSON_GetArrayItem(json,i)->valuestring;
                break;
            case 3:
                m_strSdkVersion = cJSON_GetArrayItem(json,i)->valuestring;
                break;
            case 4:
                m_strSerial = cJSON_GetArrayItem(json,i)->valuestring;
                break;
            case 5:
                m_strModel = cJSON_GetArrayItem(json,i)->valuestring;
                break;    
            default:
                break;
            }
       }
    }
    if(m_cbc.getQueue() != NULL) {
        sendResult();
    }
    return true;
}
